// pages/test/skills2.js
let testCards = require('../../data/testCards.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentStep: 1,
    currentCardIndex: 0,
    currentCard: {},
    cards: [],
    result: [0, 0, 0, 0],
    showCards: [],
    showResult: false,
    showResultTitle: '结果显示'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cards = testCards.skills
    var cardsLength = cards.length
    var sorts = ['A', 'B', 'C', 'D']
    for (var i = 0; i < cardsLength; i++) {
      cards[i].sort = sorts[i % 4]
    }
    this.setData({
      cards: cards,
      currentCard: cards[0]
    })
    var result = [0, 0, 0, 0]
    for (i = 0; i < this.data.cards.length; i++) {
      var card = this.data.cards[i]
      if (card.sort === 'A') {
        result[0]++
      } else if (card.sort === 'B') {
        result[1]++
      } else if (card.sort === 'C') {
        result[2]++
      } else {
        result[3]++
      }
    }
    this.setData({
      result: result
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onBtnTap: function(event) {
    var dtype = event.currentTarget.dataset.type
    this.data.cards[this.data.currentCardIndex].sort = dtype
    var card = this.data.cards[this.data.currentCardIndex]
    this.setData({
      cards: this.data.cards
    })
    if (this.data.currentCardIndex === this.data.cards.length - 1) {
      this.toStep1()
      return
    }
    this.nextCard()
  },
  onPreTap: function() {
    if (this.data.currentCardIndex === 0) {
      wx.showToast({
        title: '前面没有卡片额',
        icon: 'none'
      })
      return
    }
    this.preCard()
  },
  onNextTap: function() {
    var currentCard = this.data.cards[this.data.currentCardIndex]
    if (!currentCard.sort) {
      wx.showToast({
        title: '当前卡片还没有分配，请选择',
        icon: 'none'
      })
      return
    }
    var nextCardIndex = this.data.currentCardIndex + 1
    if (nextCardIndex >= this.data.cards.length) {
      wx.showToast({
        title: '后面没有卡片了额',
        icon: 'none'
      })
      return
    }
    this.nextCard()
  },
  preCard: function() {
    if (this.data.currentCardIndex  === 0) {
      return
    }
    var preCardIndex = this.data.currentCardIndex - 1
    if (preCardIndex < this.data.cards.length) {
      this.setData({
        currentCardIndex: preCardIndex,
        currentCard: this.data.cards[preCardIndex]
      })
    }
  },
  nextCard: function() {
    var nextCardIndex = this.data.currentCardIndex + 1
    if (nextCardIndex < this.data.cards.length) {
      this.setData({
        currentCardIndex: nextCardIndex,
        currentCard: this.data.cards[nextCardIndex]
      })
    }
  },
  toStep1: function() {
    var result = [0, 0, 0, 0]
    for (var i = 0; i < this.data.cards.length; i++) {
      var card = this.data.cards[i]
      if (card.sort === 'A') {
        result[0]++
      } else if (card.sort === 'B') {
        result[1]++
      } else if (card.sort === 'C') {
        result[2]++
      } else {
        result[3]++
      }
    }
    this.setData({
      result: result,
      currentStep: 1
    })
  },
  showResult: function(event) {
    this.setData({
      showCards: []
    })
    var dtype = event.currentTarget.dataset.type
    var dname = event.currentTarget.dataset.name
    var cardsLength = this.data.cards.length
    var showCards = []
    for (var i = 0; i < cardsLength; i++) {
      var card = this.data.cards[i]
      if (card.sort === dtype) {
        showCards.push(card)
      }
    }
    this.setData({
      showCards: showCards,
      showResult: true,
      showResultTitle: dname
    })
  },
  onHideResultTap: function(event) {
    this.setData({
      showResult: false,
      showResultTitle: "结果显示"
    })
  }
})